using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using CourseSync.LibFetcher.Fetcher;

namespace CourseSync.LibFetcher.Filter
{
    public class Filter
    {
        public virtual MatchCollection GetMatches(string content)
        {
            return null;
        }

        public Match GetMatch(string content)
        {
            MatchCollection matches = GetMatches(content);
            if ((matches == null) || (matches.Count <= 0))
                return null;
            else
                return matches[0];
        }

        public MatchCollection GetMatchesEx(string content)
        {
            return GetMatches(content);
        }

        public string[] GetFields(string content)
        {
            MatchCollection matches = GetMatchesEx(content);
            if (matches == null || matches.Count < 1)
                return null;
            else
                return GetFields(matches[0]);
        }

        public int[] GetIntFields(string content)
        {
            string[] fields = GetFields(content);
            if (fields == null)
                return null;
            int[] retval = new int[fields.Length];
            for (int i = 0; i < fields.Length; i++)
            {
                try
                {
                    retval[i] = int.Parse(fields[i]);
                }
                catch
                {
                    retval[i] = 0;
                }
            }
            return retval;
        }

        public string[][] GetFieldsList(string content)
        {
            MatchCollection matches = GetMatchesEx(content);
            string[][] retval = new string[matches.Count][];
            for (int i = 0; i < matches.Count; i++)
                retval[i] = GetFields(matches[i]);
            return retval;
        }

        protected string[] GetFields(Match match)
        {
            if (match == null)
                return new string[0];
            if (!match.Success)
                return new string[0];

            if (match.Groups.Count > 1)
            {
                string[] retval = new string[match.Groups.Count - 1];
                for (int i = 1; i < match.Groups.Count; i++)
                    retval[i - 1] = match.Groups[i].Value;
                return retval;
            }
            else
            {
                string[] retval = new string[1];
                retval[0] = match.Groups[0].Value;
                return retval;
            }
        }

        public int GetCount(string content)
        {
            MatchCollection matches = GetMatchesEx(content);
            return matches.Count;
        }

        public string[] GetValues(string content)
        {
            MatchCollection matches = GetMatchesEx(content);
            if (matches == null)
                return null;
            string[] retval = new string[matches.Count];
            for (int i = 0; i < matches.Count; i++)
            {
                if (matches[i].Groups.Count > 1)
                    retval[i] = matches[i].Groups[1].Value;
                else
                    retval[i] = matches[i].Groups[0].Value;
            }
            return retval;
        }

        public int[] GetIntValues(string content)
        {
            MatchCollection matches = GetMatchesEx(content);
            if (matches == null)
                return null;

            int[] retval = new int[matches.Count];
            for (int i = 0; i < matches.Count; i++)
            {
                try
                {
                    if (matches[i].Groups.Count > 1)
                        retval[i] = int.Parse(matches[i].Groups[1].Value);
                    else
                        retval[i] = int.Parse(matches[i].Groups[0].Value);
                }
                catch
                {
                    retval[i] = 0;
                }
            }
            return retval;
        }


        public string GetValue(string content)
        {
            string[] values = GetValues(content);

            if (values == null || values.Length < 1)
                return null;
            else
                return values[0];
        }

        public int GetIntValue(string content)
        {
            string str = GetValue(content);
            if (str == null)
                return 0;
            try
            {
                return int.Parse(str);
            }
            catch
            {
                return 0;
            }
        }
    }
}
